const router = require('express').Router();
const otherService = require('../../service/other');
const joi = require('joi');
const joiValidate = require('../../utils/joiValidate');
const verifyUser = require('../../utils/jwtHanlder');
/**
 * @apiDefine Other  获取各种设置
 */

/**
 * @api {get} /v1/other/base  获取基础的微信号手机号等
 * @apiVersion 1.0.0
 * @apiName GetOtherBase
 * @apiGroup  Other
 *
 * @apiDescription 获取基础的微信号手机号，协议等
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/other/base
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 * @apiSuccess {Object[]} data    返回值数据
 * @apiSuccess {Object[]} data.detail  数组
 * @apiSuccess {number} data.detail.wechat  微信号
 * @apiSuccess {string} data.detail.phone  电弧号码
 * @apiSuccess {number} data.detail.userProtocel  协议
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功",
    "data": {
        "detail": {
            "wechat": "12132",
            "phone": "",
            "userProtocel": "fdasf"
        }
    }
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.get('/base', (req, res, next) => {
  otherService.getBaseDesign()
    .then((data) => res.success({ detail: data }))
    .catch(e => next(e))
});

/**
 * @api {post} /v1/other/write/message 写留言板
 * @apiVersion 1.0.0
 * @apiName PostOtherMsg
 * @apiGroup  Other
 *
 * @apiDescription 写留言板
 *
 * @apiParam {string} message  留言
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/other/write/message
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功"
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.post('/write/message', verifyUser.verify(), joiValidate(joi.object().keys({
  message: joi.string().required().label('留言')
})), (req, res, next) => {
  const { message } = req.body;
  const { userId } = req.user;
  otherService.writeMessage({ message, userId })
    .then(() => res.success())
    .catch(e => next(e))
});

/**
 * @api {get} /v1/other/bank/list  获取银行列表
 * @apiVersion 1.0.0
 * @apiName GetOtherBankList
 * @apiGroup  Other
 *
 * @apiDescription 获取可使用的银行列表
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/other/bank/list
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 * @apiSuccess {Object[]} data    返回值数据
 * @apiSuccess {Object[]} data.list  数组
 * @apiSuccess {number} data.list.id  银行Id
 * @apiSuccess {string} data.list.bankName  银行名称
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功",
    "data": {
        "list": [
            {
                "id": 1,
                "bankName": "中国工商银行"
            },
            {
                "id": 2,
                "bankName": "中国农业银行"
            },
            {
                "id": 3,
                "bankName": "中国建设银行"
            },
            {
                "id": 4,
                "bankName": "中国交通银行"
            },
            {
                "id": 5,
                "bankName": "中国银行"
            }
        ]
    }
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.get('/bank/list', (req, res, next) => {
  otherService.getBankList()
    .then((data) => res.success(data))
    .catch(e => next(e))
});


/**
 * @api {post} /v1/other/bank/add  添加银行
 * @apiVersion 1.0.0
 * @apiName PostBankAdd
 * @apiGroup  Other
 *
 * @apiDescription 添加银行
 * @apiParam {number} bankId  银行Id
 * @apiParam {string} bankNo  银行账号
 * @apiParam {string} realName  真实名称
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/other/bank/add
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功"
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.post('/bank/add', verifyUser.verify(), joiValidate(joi.object().keys({
  bankId: joi.number().required().label('请选择银行'),
  bankNo: joi.string().required().label('请输入银行账号'),
  realName: joi.string().required().label('请输入真实名称'),
})),(req, res, next) => {
  const { bankId, bankNo, realName } = req.body;
  const { userId } = req.user;
  otherService.addBankInfo({ bankId, bankNo, realName, userId })
    .then(() => res.success())
    .catch(e => next(e))
});

/**
 * @api {get} /v1/other/user/bank  获取用户银行信息
 * @apiVersion 1.0.0
 * @apiName GetUserBankInfo
 * @apiGroup  Other
 *
 * @apiDescription 获取用户银行信息
 *
 * @apiExample Example usage:
 * curl -i http://localhost:3502/v1/other/user/bank
 *
 * @apiSuccess {Number} code      请求结果编码，0：正常， 其他为异常
 * @apiSuccess {string} message    请求结果编码对应的message
 * @apiSuccess {Object} data    请求结果编码对应的message
 * @apiSuccess {Object} data.detail    请求结果编码对应的message
 * @apiSuccess {number} data.detail.userId    用户Id
 * @apiSuccess {number} data.detail.id    id
 * @apiSuccess {string} data.detail.bankNo    银行账号
 * @apiSuccess {string} data.detail.realName    真实名称
 * @apiSuccess {string} data.detail.bankName    银行名称
 *
 * @apiSuccessExample 正常响应：
 *     HTTP/1.1 200
 {
    "code": 200,
    "message": "请求成功",
    "data": {
        "detail": {
            "userId": 32,
            "id": 4,
            "bankNo": "x",
            "bankId": 1,
            "realName": "d",
            "bankName": "中国工商银行"
        }
    }
}
 *
 *
 * @apiError  ServerError  服务器异常
 *
 * @apiErrorExample Response (example):
 *     HTTP/1.1 500 ServerError
 *     {
 *       "code": 500,
 *       "message": "服务器异常"
 *     }
 */
router.get('/user/bank', verifyUser.verify(),(req, res, next) => {
  const { userId } = req.user;
  otherService.getBankInfo({ userId })
    .then((data) => res.success({ detail: data }))
    .catch(e => next(e))
});

module.exports = router;
